#include "HeartHolder.h"
#include "AnimationFilmHolder.h"
#include "CollisionChecker.h"

std::list<Hearts *> HeartHolder::hearts;
bool HeartHolder::arePaused;
int HeartHolder::inForCheck;
TrophyDisplay *HeartHolder::trophyDisplay;
bool HeartHolder::heartBroken;

void HeartHolder::Initialize(void) {
	inForCheck = 0;
	arePaused = false;
	heartBroken = false;
}

void HeartHolder::NewHeart(Dim x, Dim y, AnimationFilm* film, animid_t id) {
	int total_frames = film->GetTotalFrames();
	HeartHolder::arePaused = false;

	FrameRangeAnimation *fallAnimation = new FrameRangeAnimation(0, total_frames-1, 0, 5, 200, true, id);
	FrameRangeAnimator *fallAnimator = new FrameRangeAnimator();
	AnimatorHolder::Register(fallAnimator);
	
	FlashingAnimation *flashAnimation = new FlashingAnimation(1, 250, 600, 0);
	FlashingAnimator *flashAnimator = new FlashingAnimator();
	AnimatorHolder::Register(flashAnimator);

	Hearts *heart_s = new Hearts(x, y, film, id, fallAnimation, fallAnimator, 
		flashAnimation, flashAnimator, trophyDisplay);
	hearts.push_back(heart_s);
	
	heart_s->SetVisibility(true);
	heart_s->StartFalling();
}

std::list<Hearts *> HeartHolder::getHeartList(void) {
	return hearts;
}

void HeartHolder::DisplayAll(void) {
	std::list<Hearts *>::iterator it;
	for(it=hearts.begin(); it!=hearts.end(); ++it) {
		if((*it)->IsVisible())
			(*it)->Display(Rect(0,0,0,0));
	}
}

void HeartHolder::Remove(Hearts* h) {
	inForCheck--;
	hearts.remove(h);
}

bool HeartHolder::Flashing(void) {
	std::list<Hearts *>::iterator it;
	
	for(it=hearts.begin(); it!=hearts.end(); ++it) {
		if((*it)->Flashes())
			return true;
	}
	return false;
}

bool HeartHolder::spinachEnabled(void) {
	return arePaused;
}

void HeartHolder::PauseHearts(timestamp_t currtime) {
	std::list<Hearts *>::iterator it;
	for(it=hearts.begin(); it!=hearts.end(); ++it) {
		(*it)->Pause(currtime);
	}
	arePaused = true;
}

void HeartHolder::ResumeHearts(void) {
	std::list<Hearts *>::iterator it;
	for(it=hearts.begin(); it!=hearts.end(); ++it) {
		(*it)->Resume();
	}
	arePaused = false;
}
